package com.syq.face.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.syq.face.R;
import com.syq.face.model.UserInfo;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AccountInfoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AccountInfoFragment extends NotifiedFragment {

    public static final String TAG = "AccountInfoFragment";
    private static final String USER_INFO = "userinfo";

    private TextView user_name_text;
    private TextView nick_name_text;
    private TextView email_text;
    private TextView phone_text;

    private String userInfo;

    public void setUserInfo(String userInfo) {
        this.userInfo = userInfo;
    }



    public AccountInfoFragment() {

    }


    public static AccountInfoFragment newInstance(UserInfo userInfo) {

        AccountInfoFragment fragment = new AccountInfoFragment();
        Bundle args = new Bundle();
        Log.d(TAG, "newInstance: userinfo"+userInfo);
        args.putString(USER_INFO, new Gson().toJson(userInfo));
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            userInfo = getArguments().getString(USER_INFO);
            Log.d(TAG, "onCreate: userinfo"+userInfo);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_account_info, container, false);
        init(rootView);

        return rootView;
    }

    private void loadData() {
        String json=userInfo;
        Gson gson=new Gson();
        UserInfo info=gson.fromJson(json,UserInfo.class);
        Log.d(TAG, "loadData: info"+info);
        user_name_text.setText("用户名: "+info.getUsername());
        nick_name_text.setText("昵称： "+info.getNickname());
        email_text.setText("邮箱： "+info.getEmail());
        phone_text.setText("电话： "+info.getPhone());

    }

    private void init(View rootView) {

        user_name_text=rootView.findViewById(R.id.user_name);
        nick_name_text=rootView.findViewById(R.id.nick_name_text);
        email_text=rootView.findViewById(R.id.email_text);
        phone_text=rootView.findViewById(R.id.phone_text);
        loadData();
    }

    @Override
    public void notifyDataReload() {

    }
}